﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YSC_Core.BLL.Domain.CommonModels;
using YSC_Core.BLL.Domain.Entity;
using YSC_Core.BLL.Domain.Utils;
using YSC_Core.BLL.Domain.ViewModels;
using YSC_Core.BLL.Repository;
using YSC_OpenPlatform_API.Entity;
using YSC_Services.BLL.Services_Web.Interfaces;

namespace YSC_Services.BLL.Services_Web.Implementations
{
    public class BasisAppServices : IBasisAppServices
    {
        private IEfRepository _rep;

        public BasisAppServices(IEfRepository rep)
        {
            this._rep = rep;
        }

        /// <summary>
        /// 地区列表
        /// </summary>
        /// <param name="level">等级:1省份2市3区</param>
        /// <param name="code">上级编码</param>
        /// <param name="name">城市名称</param>
        /// <returns></returns>
        public ReturnValueModel GetAreaAll(int level, string code, string name)
        {
            var areaList = _rep.Where<Area>(p => level == 0 ? p.Level != 3 : p.Level == level).Select(p => new
            {
                p.YunCode,
                p.AreaName,
                p.PyCode,
                p.PunCode,
                p.Level
            })
            .WhereIf(p => p.PunCode == code, string.IsNullOrEmpty(code) == false)
            .WhereIf(p => p.AreaName.Contains(name) || name.Contains(p.AreaName), string.IsNullOrEmpty(name) == false)
            .OrderByDescending(p => p.AreaName)
            .ToList();


            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = areaList } };
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ReturnValueModel GetDictionaryAll(string type)
        {
            var dictionaryList = _rep.Where<Dictionary>(p => p.Type == type).Select(p => new
            {
                p.Id,
                p.Name
            })
           .OrderByDescending(p => p.Name)
           .ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = dictionaryList } };
        }

        /// <summary>
        /// 用户单位列表
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public ReturnValueModel GetMarketUnitAll(string userId)
        {
            if (string.IsNullOrEmpty(userId))
                return new ReturnValueModel { Code = 401, Msg = "用户凭证过期!" };

            var userItem = _rep.FirstOrDefault<YscUser>(p => p.Id == userId);

            if (userItem.IsAttest == false)
                return new ReturnValueModel { Code = 401, Msg = "用户没有权限!" };

            var dictionary = _rep.FirstOrDefault<Dictionary>(p => p.Id == userItem.AttestType);

            if (!dictionary.Name.Contains("产地"))
            {
                var marketUnitAll = from a in _rep.Where<UserUnit>(p => p.Status == 0)
                                    join m in _rep.Where<MarketUnit>(p => p.Status == 0) on a.UnitId equals m.Id
                                    select new
                                    {
                                        m.Id,
                                        a.UserId,
                                        m.MarketName,
                                        m.Address,
                                        Type = "市场"
                                    };

                var marketUnitList = marketUnitAll
                    .WhereIf(p => p.UserId == userId, string.IsNullOrEmpty(userId) == false)
                    .ToList();

                return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = marketUnitList } };
            }

            var placeOfOriginAll = from a in _rep.Where<UserUnit>(p => p.Status == 0)
                                   join m in _rep.Where<PlaceOfOriginUnit>(p => p.Status == 0) on a.UnitId equals m.Id
                                   select new
                                   {
                                       m.Id,
                                       a.UserId,
                                       MarketName = m.PlaceOriginName,
                                       m.Address,
                                       Type = "产地"
                                   };

            var placeOfOriginList = placeOfOriginAll
                .WhereIf(p => p.UserId == userId, string.IsNullOrEmpty(userId) == false)
                .ToList();


            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = placeOfOriginList } };
        }

        /// <summary>
        /// 鱼种列表
        /// </summary>
        /// <param name="fishType">鱼种类别</param>
        /// <returns></returns>
        public ReturnValueModel GetFingerlingTypeAll(string fishType)
        {
            var fingerlingTypeList = _rep.Where<FishSpecies>(p => p.Status == 0)
                                        .Join(_rep.Where<Dictionary>(p => true), a => a.Type, d => d.Id, (a, d) => new { Type = d.Name, a.FishName, a.FishCode })
                .GroupBy(p => new { p.Type })
                .Select(p => new FingerlingTypeModel
                {
                    Type = p.Key.Type,
                    FishAll = p.Select(f => new FingerlingTypesModel { FishName = f.FishName, FishCode = f.FishCode }).ToList().DistinctBy(s => s.FishName).ToList()
                }).ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = fingerlingTypeList } };
        }

        /// <summary>
        /// 鱼种规格
        /// </summary>
        /// <param name="fishName"></param>
        /// <returns></returns>
        public ReturnValueModel GetDesciptionAll(string fishName)
        {
            if (string.IsNullOrEmpty(fishName))
                return new ReturnValueModel { Code = 400, Msg = "鱼种编码,填写错误！" };
            var DesciptionList = _rep.Where<FishSpecies>(p => p.FishName == fishName).Select(p => new { p.FishCode, Description = p.Description.Trim() }).DistinctBy(p => p.Description).ToList();

            return new ReturnValueModel { Code = 200, Msg = "success", Result = new { Data = DesciptionList } };
        }
    }
}
